Update AddHoldInvoice to add support for optional preimage/hash generation#10685
Update AddHoldInvoice to add support for optional preimage/hash generation#10685saubyk wants to merge 4 commits intolightningnetwork:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the AddHoldInvoice functionality by introducing more flexible preimage and hash management. It allows users to either provide a preimage for the server to derive a hash from, or opt for the server to auto-generate both a preimage and hash. The changes include updates to the RPC interface, command-line tool, and server-side logic, ensuring backward compatibility while providing robust error handling for invalid configurations. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request enhances the AddHoldInvoice RPC and CLI command to support optional hash and preimage parameters. Users can now provide a preimage directly (from which the server derives the hash), provide a hash as before, or provide neither to have the server auto-generate both. The changes include updates to the protobuf definitions, CLI flag handling, server-side logic, and a comprehensive integration test suite. I have no feedback to provide.
bb700f6 to
e2fac2a
Compare
|
Design question: should Thanks for the PR — the auto-generate feature is a real quality-of-life improvement and the core implementation is solid. I want to raise two related design questions before this lands. 1. Should The stated purpose is to let callers avoid computing In exchange, the feature:
The original hold invoice design was deliberate: the merchant keeps the preimage locally, passes only the hash to The correct flow for a caller who already has a preimage is just: My suggestion: remove 2. Should the preimage be stored in the DB even for the auto-generate case? Currently even the auto-generate path stores the preimage in The cleaner approach: generate the preimage locally in default: // auto-generate
var preimage lntypes.Preimage
if _, err := rand.Read(preimage[:]); err != nil {
return nil, fmt.Errorf("failed to generate preimage: %w", err)
}
h := preimage.Hash()
hashPtr = &h
autoPreimage = &preimage // returned in response, not storedThis preserves the hold invoice invariant: the DB only ever holds the hash, never the preimage. The preimage is returned once in the response (the caller must store it, like a seed phrase), and provided back at settle time. It also eliminates a whole class of future bugs where a preimage stored in the DB could be used inadvertently if the To be clear — none of this blocks the auto-generate feature itself, which is good and should land. The question is just whether we want to keep |
Make the hash field optional in AddHoldInvoiceRequest. When the hash is omitted, the server generates a random preimage locally, derives the payment hash, and returns the preimage in the response. The generated preimage is not passed to AddInvoice, so it is never persisted in the invoice DB — the caller must save the returned value and supply it to SettleInvoice later. This preserves the hold-invoice invariant that lnd only learns the preimage at settle time. The response adds payment_preimage (populated only for the auto-generated case) and payment_hash (always populated), giving callers the info needed to drive the hold-invoice lifecycle.
Add a --hash flag to the lncli addholdinvoice command, matching the new optional-hash behavior on the RPC. The old positional hash argument is kept for backward compatibility (detected by 64 hex char length). When no hash is provided the server auto-generates the preimage/hash pair and returns the preimage in the response for the caller to save.
Add integration test covering the two AddHoldInvoice input modes: auto-generate (no hash provided) and hash-only (backward compat). For the auto-generate case, the test also verifies via LookupInvoice that the generated preimage is not persisted in the invoice DB. Each scenario exercises the full payment lifecycle including settle.
Add release notes entry for the AddHoldInvoice optional-hash feature under RPC Updates and lncli Updates.
e2fac2a to
f72a89f
Compare
|
🟠 PR Severity: HIGH | 5 files | 217 lines changed | HIGH: lnrpc/invoicesrpc/invoices_server.go (lnrpc/* RPC server) | MEDIUM: cmd/commands/invoicesrpc_active.go (cmd/* CLI), lnrpc/invoicesrpc/invoices.proto (.proto), lnrpc/invoicesrpc/invoices.swagger.json | LOW: docs/release-notes/release-notes-0.22.0.md | EXCLUDED (test/generated): itest/list_on_test.go, itest/lnd_hold_invoice_auto_generate_test.go, lnrpc/invoicesrpc/invoices.pb.go | No bump: 5 files under threshold 20, 217 lines under threshold 500. To override add severity-override-{critical,high,medium,low} label. <!-- pr-severity-bot --> |
|
Hi @ziggie1984 @yyforyongyu the feedback has been addressed. The pr is ready for your review. Thanks |
|
@yyforyongyu: review reminder |
I don't understand this. I don't see |
| caller provided a hash, since the server does not know the | ||
| preimage in that case. | ||
| */ | ||
| bytes payment_preimage = 4; |
There was a problem hiding this comment.
Should we return this? We don't do so for normal (non-hold) invoices. If we do want to return this, I think it should be based on a macaroon scope.
I think it should. We already do this for normal (non-hold) invoices. Also, requiring the client to persist the preimage requires that client to persist more state that they may not want to do (which we don't require for normal (non-hold) invoices). I think the state should be held within LND if the preimage is generated by LND. If there is some use case where we don't want LND to know the preimage before we are ready to settle, there is no way to prove that LND forgot the preimage if LND generate the preimage, so I see no advantage in LND not saving the preimage if it generated it. Additionally, if LND saves the preimage, we can have another option to auto-settle after X number of blocks, rather than only be able to auto-cancel. Or, allow another client that did not generate the preimage (or receive the preimage) to settle the invoice. This could help with users of #3120 who want to deal with hold invoices that may have one client dealing with all hold invoices on a node. Generally, my understanding of the current design was that when we force the client to generate the preimage themselves, we are sort of forcing them to realize that they are creating something unique and they need to persist it in order to be able to settle the hold invoice. If we instead generate the preimage in LND and return it to the client, but LND forgets the preimage, the client may not realize they needed to persist the preimage that was returned to them. I definitely think we should still give the user the option to generate and persist their own preimage if they have unique application that requires it to be done externally. If we are going to give the functionality where they don't have to generate the preimage on their own (which is a nice alternative to give users), we should also not require them to persist it. |
|
I am generally not sure about this feature as a whole now. Hold-Invoices and normal invoices are by design separated. Hold-Invoices need to be settled or canceled via an external API (will be canceled after a timeout). So when setteling the invoice the caller needs to provide the preimage anyways so I would still push back persisting the preimage here. Providing the autogeneration of the preimage and hash can make sense to define the standards for the caller but persisting the preimage I would not do for now. |
Fixes #7220
Change Description
Update AddHoldInvoice to add support for optional preimage/hash generation. Following scenarios are handled:
Steps to Test
<hash>10000 (legacy positional arg still works)<hex>10000 (new flag)Pull Request Checklist
Testing
Code Style and Documentation
[skip ci]in the commit message for small changes.📝 Please see our Contribution Guidelines for further guidance.